import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { MessageService } from '@core/rsocket/message.service'
import { StorageService } from '@core/storage/storage.sercvice';
import { SettingsService, _HttpClient } from '@delon/theme';
import { environment } from '@env/environment';

@Component({
  selector: 'app-one',
  templateUrl: 'one.component.html'
})
export class OneChatComponent implements OnInit {
  activeUserId;
  activeYourUserId;
  one: any = {};
  content = '';

  messages: any[] = [{fromId: 1, content: '123213211fsdgfdas'}];

  constructor(
    private routeInfo: ActivatedRoute,
    private messageService: MessageService,
    private settingService: SettingsService,
    private storageService: StorageService,
    private http: _HttpClient,
  ) {
    this.activeYourUserId = this.routeInfo.snapshot.params.id;
    this.activeUserId = this.settingService.user.id;
    // this.storageService.get("messages").then(res =>{
    //   if(res){
    //     this.messages = res;
    //   }else{
    //     this.http.get(`${environment.im.ctx}/imMessage/findByFromIdAndToId`, {toId: this.activeYourUserId}).subscribe((res: any) => {
    //       this.messages = res.list;
    //     })
    //   }
    // })

    this.messageService.receive$.subscribe(res => {
      if(res.receiver == this.activeUserId){
        this.messages.push(res);
      }
     
    });

   
  }

  ngOnInit() {}

  send() {
    this.messageService.send(this.activeUserId, this.activeYourUserId, this.content)
      .subscribe({
        onComplete: response => {
          const data = response.data;
          if (data) {
            console.log(`Requester response: ${data}`);
          }
        },
        onError: error =>
          console.log(`Requester error: ${error.message}`),
      });
  }



}

